document.ready(function() {
    webSpinner.spin(getItem('loadSpin'));
    listSpinner.spin(getItem('loading'));

    loadFocusBar();

    if (getStorage('recommend_id') != null && getStorage('recommend_id') != '') {
        //详情页返回加载
        if (parseInt(getStorage('recommend_page')) > 10) {
            setStorage('recommend_page', '10');
        }
        resetList();
    }
    else {
        //首次进入加载
        queryKey('ok');
    }
});

function resetList() {
    resetBottomStatus('loading');

    var param = {};
    param.team = 'platform';
    param.ctrl = 'home';
    param.func = 'resetRecommendList';
    param.tabtype = encodeURIComponent(getStorage('recommend_tabtype'));
    param.page = encodeURIComponent(getStorage('recommend_page'));

    sendAjax(param, function(result) {
        var list = result.recommendList;
        recommend.list = list;

        resetBottomStatus('reset', 19, list);

        setDisplay('loadSpin', 'none');
        setDisplay('mainBg', '');
        webSpinner.spin();
    });
}

function queryKey(flag) {
    resetPageBar(flag);
    resetBottomStatus('loading');

    var param = {};
    param.team = 'platform';
    param.ctrl = 'home';
    param.func = 'getRecommendList';
    param.tabtype = getAjaxValue('tabtype');
    param.page = getAjaxValue('page');

    sendAjax(param, function(result) {
        var list = result.recommendList;
        if (getValue('page') == '1' || recommend.list == '') {
            recommend.list = list;
            location.href = "#mainBg";
            setValue('page', '1');
        }
        else {
            if (list.length > 0) {
                for (var i = 0; i < list.length; i++) {
                    recommend.list.push(list[i]);
                }
            }
        }

        resetBottomStatus('bottomed', 19, list, recommend.list);

        setDisplay('loadSpin', 'none');
        setDisplay('mainBg', '');
        webSpinner.spin();
    });
}

function viewRecommend(id, datatype) {
    setStorage('recommend_page', getValue('page'));
    setStorage('recommend_id', id);
    setStorage('recommend_tabtype', getValue('tabtype'));

    if (datatype == 'electronic') {
        toUrl('index.php?m=electronic&c=info&from=home&electronicid=' + id);
    }
    else if (datatype == 'housecraft') {
        toUrl('index.php?m=housecraft&c=info&from=home&housecraftid=' + id);
    }
    else if (datatype == 'other') {
        toUrl('index.php?m=other&c=info&from=home&otherid=' + id);
    }
    else {
        showTip('栏目分类异常');
    }
}

function resetOption() {
    //重置推荐分类样式
    var tabtype = getValue('tabtype');
    if (tabtype == '0') {
        tabtype = '专属推荐';
    }

    var objList = getItem('tabOption').children;
    objList[0].className = 'tab-option';
    for (var i = 0; i < objList.length; i++) {
        if (tabtype.indexOf(objList[i].innerHTML) != -1) {
            objList[i].className = 'tab-option-click';
            break;
        }
    }
}